package com.mylib.libcore.utils.crc;

import android.util.Base64;

import com.mylib.libcore.AppContext;
import com.mylib.libcore.utils.JsonUtil;
import com.mylib.libcore.utils.UIUtils;

import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import static com.mylib.libcore.config.NetConfig.NET_IS_LOCAL;


/**
  * @Description: aes加密工具类
  * @author: lrfalse
  * @date : 2019/12/19 19:41
  **/
public class AESUtils {

	private String sKey = "OQ7sw$IOK1@$P$Ar";
	private String ivParameter = "1571022675911675";
	private static AESUtils instance = null;

	private AESUtils() {

	}

	public static AESUtils getInstance() {
		if (instance == null)
			instance = new AESUtils();
		return instance;
	}

	public static String Encrypt(String encData , String secretKey, String vector) throws Exception {

		if(secretKey == null) {
			return null;
		}
		if(secretKey.length() != 16) {
			return null;
		}
		Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
		byte[] raw = secretKey.getBytes();
		SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
		IvParameterSpec iv = new IvParameterSpec(vector.getBytes());// 使用CBC模式，需要一个向量iv，可增加加密算法的强度
		cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
		byte[] encrypted = cipher.doFinal(encData.getBytes("utf-8"));
		return   Base64.encodeToString(encrypted, Base64.DEFAULT);// 此处使用BASE64做转码。
	}


	// 加密
	public String encrypt(HashMap<String, String> hashMap)  {
		hashMap.put("wdt", AppContext.getInstance().getAppPreferencesHelper().getToken()+"");
		if(NET_IS_LOCAL){
			return JsonUtil.toJson(hashMap);
		}else {
			String sSrc = JsonUtil.toJson(hashMap);
			String encryptString = "";
			try {
				Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
				byte[] raw = sKey.getBytes();
				SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
				IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());// 使用CBC模式，需要一个向量iv，可增加加密算法的强度
				cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
				byte[] encrypted = cipher.doFinal(sSrc.getBytes("utf-8"));
				encryptString =  Base64.encodeToString(encrypted, Base64.DEFAULT);// 此处使用BASE64做转码。
			} catch (NoSuchAlgorithmException e) {
				e.printStackTrace();
			} catch (NoSuchPaddingException e) {
				e.printStackTrace();
			} catch (InvalidAlgorithmParameterException e) {
				e.printStackTrace();
			} catch (InvalidKeyException e) {
				e.printStackTrace();
			} catch (BadPaddingException e) {
				e.printStackTrace();
			} catch (IllegalBlockSizeException e) {
				e.printStackTrace();
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			return encryptString;
		}
	}

	// 解密
	public String decrypt(Object object)  {
		if(UIUtils.isEmpty(object))return "";
		if(NET_IS_LOCAL){
			return object.toString();
		}else {
			String sSrc = object.toString();
			try {
				byte[] raw = sKey.getBytes("ASCII");
				SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
				Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
				IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());
				cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
				byte[] encrypted1 = Base64.decode(sSrc, Base64.DEFAULT);// 先用base64解密
				byte[] original = cipher.doFinal(encrypted1);
				String originalString = new String(original, "utf-8");
				return originalString;
			} catch (Exception ex) {
				return "";
			}
		}

	}

	public String decrypt(String sSrc, String key, String ivs) throws Exception {
		try {
			byte[] raw = key.getBytes("ASCII");
			SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
			Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
			IvParameterSpec iv = new IvParameterSpec(ivs.getBytes());
			cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
			byte[] encrypted1 =  Base64.decode(sSrc, Base64.DEFAULT);// 先用base64解密
			byte[] original = cipher.doFinal(encrypted1);
			String originalString = new String(original, "utf-8");
			return originalString;
		} catch (Exception ex) {
			return null;
		}
	}

	public static String encodeBytes(byte[] bytes) {
		StringBuffer strBuf = new StringBuffer();

		for (int i = 0; i < bytes.length; i++) {
			strBuf.append((char) (((bytes[i] >> 4) & 0xF) + ((int) 'a')));
			strBuf.append((char) (((bytes[i]) & 0xF) + ((int) 'a')));
		}

		return strBuf.toString();
	}



}